<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;
use think\Controller;

/**
 * 
 */
class Common extends Controller
{
	
	public function getRegion()
	{
		if (Request::instance()->isPOST())
		{ 
			$pcode = Request::instance()->post('pcode');//父编码
			$level = Request::instance()->post('level');//等级
			if($level==1){
                $data['list'] = db::name('cn_city')->field('code,name')->where('pcode',$pcode)->select();
			}else{
				$data['list'] = db::name('cn_area')->field('code,name')->where('pcode',$pcode)->select();
			}

			$data['code']=1;
		}
		else{
            $data['code']=0;
		}
		echo json_encode($data);
		exit;
	}


	public function getInfo()
	{
		if(Request::instance()->isPost())
		{
			$id = Request::instance()->post('id'); 
			$list['list'] = DB::name('user')->field('id,truename')->where('is_del = 0 and id <>3 and shop_id = '.$id)->select();
			$list['code'] = 1; 
		}else{
			$list['code'] = 0;
		}
		echo json_encode($list);
		exit();
	}


	    //上传图片
	public function upload(){
		// 获取表单上传文件 例如上传了001.jpg
		$file = request()->file('file_data');
		// 移动到框架应用根目录/public/uploads/ 目录下
		$info = $file->move(ROOT_PATH . 'public' . DS . 'tempdir');
		if($info){
			$date = date('Ymd');
			$data['code'] = 0;
			$data['img'] = '/public/tempdir/'.$date.'/'.$info->getFilename();
			// 成功上传后 获取上传信息
			// 输出 jpg
			//echo $info->getExtension();
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			//echo $info->getSaveName();
			// 输出 42a79759f284b767dfcb2a0197904287.jpg
			//echo $info->getFilename(); 
		}else{
			// 上传失败获取错误信息
			//echo $file->getError();
			$data['code'] = -1;
			$data['error'] = $info->getError();
		}
		return json_encode($data);
	}

	//删除图片
	public function updel(){
		$filename = input('filename');
			if (!empty($filename)) {
				@unlink(ROOT_PATH . 'public'. DS .$filename);
				$data['code']=1;
			} else {
				$data['code']=2;
			}
		return json_encode($data);

	}

}